WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
composite.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21#include "core/common/wolf.h"
22#include <string>
23#include <map>
24#include <algorithm>
25#include "yaml-cpp/yaml.h"
26
27namespace wolf
28{
29using std::map;
30
31typedef std::string StateKeys;
32
33static std::string ALL_KEYS = " ";
34
38StateKeys keysUnion(const StateKeys& _keys1, const StateKeys& _keys2);
39
43StateKeys keysIntersection(const StateKeys& _keys1, const StateKeys& _keys2);
44
47template <typename T>
48class Composite : public map<char, T>
49{
50 public:
51 using CompositeType = T;
52
53 using map<char, T>::map;
54
61 Composite(const YAML::Node& _n,
62 const std::string& _field,
63 const bool _required,
64 const std::string& _keys = ALL_KEYS);
65
68 virtual ~Composite() = default;
69
72 bool has(char _key) const;
73
76 bool has(const StateKeys& _structure) const;
77
81
84 friend std::ostream& operator<<(std::ostream& _os, const Composite<T>& _x)
85 {
86 for (auto&& pair : _x)
87 {
88 _os << pair.first << ": " << pair.second << std::endl;
89 }
90 return _os;
91 }
92
95 Composite operator()(const std::string& _keys) const;
96
99 YAML::Node toYaml() const;
100};
101
103
104template <typename T>
105inline Composite<T>::Composite(const YAML::Node& _n,
106 const std::string& _field,
107 const bool _required,
108 const std::string& _keys)
109{
110 if (_n.IsDefined())
111 {
112 if (not _n.IsMap())
113 {
114 throw std::runtime_error("Composite: constructor with a non-map yaml node");
115 }
116
117 // check existence of _keys in node
118 if (_keys != ALL_KEYS)
119 for (auto key : _keys)
120 if (not _n[key] and _required)
121 {
122 throw std::runtime_error(std::string("Composite ") + typeid(T).name() +
123 " constructor: Missing node for key '" + key + "'.");
124 }
125
126 // iterate node map pairs
127 for (auto node_pair : _n)
128 {
129 // check that keys are char type
130 char key;
131 try
132 {
133 key = node_pair.first.as<char>();
134 }
135 catch (const std::exception& e)
136 {
137 throw std::runtime_error(std::string("In Composite ") + typeid(T).name() +
138 " constructor: There is a key of type different than char: " +
139 node_pair.first.as<std::string>() + ". Error: " + e.what());
140 }
141
142 // discard if not in _keys (if _keys is not ALL_KEYS)
143 if (_keys != ALL_KEYS and //
144 _keys.find(key) == std::string::npos) //
145 continue;
146
147 // check field
148 if (not _field.empty())
149 {
150 if (node_pair.second[_field])
151 {
152 try
153 {
154 this->emplace(key, node_pair.second[_field].as<T>());
155 }
156 catch (const std::exception& e)
157 {
158 YAML::Emitter em;
159 em << node_pair.second[_field];
160 throw std::runtime_error("In Composite constructor: Failed to emplace the entry " + _field +
161 " for key '" + key + "' with error: " + e.what() + "\nNode:\n" +
162 em.c_str());
163 }
164 }
165 else if (_required)
166 {
167 YAML::Emitter em;
168 em << node_pair.second;
169 throw std::runtime_error("In Composite constructor: Missing mandatory field '" + _field +
170 "' for key '" + key + "'\nNode:\n" + em.c_str());
171 }
172 }
173 else
174 {
175 try
176 {
177 this->emplace(key, node_pair.second.as<T>());
178 }
179 catch (const std::exception& e)
180 {
181 YAML::Emitter em;
182 em << node_pair.second;
183 throw std::runtime_error("In Composite constructor: Failed to emplace the entry for key '" +
184 std::string(1, key) + "' with error: " + e.what() + "\nNode:\n" +
185 em.c_str());
186 }
187 }
188 }
189 }
190 else
191 {
192 WOLF_WARN("The YAML node is not defined. Constructing an empty composite");
193 }
194}
195
196template <typename T>
197inline bool Composite<T>::has(char _key) const
198{
199 return this->count(_key) != 0;
200}
201
202template <typename T>
203inline bool Composite<T>::has(const StateKeys& _query_keys) const
204{
205 for (auto&& key : _query_keys)
206 {
207 if (not has(key))
208 {
209 return false;
210 }
211 }
212 return true;
213}
214
215template <typename T>
217{
218 StateKeys keys;
219 for (auto&& pair : *this) keys.push_back(pair.first);
220
221 return keys;
222}
223
224template <typename T>
225inline Composite<T> Composite<T>::operator()(const std::string& _keys) const
226{
227 if (not this->has(_keys))
228 {
229 throw std::runtime_error("Composite<T>::operator() required keys " + _keys + " are not available, only have " +
230 getKeys());
231 }
232
233 Composite<T> output;
234 for (auto key : _keys)
235 {
236 output.emplace(key, this->at(key));
237 }
238 return output;
239}
240
241template <typename T>
242inline YAML::Node Composite<T>::toYaml() const
243{
244 return YAML::Node(*this);
245}
246
247} // namespace wolf
248
249// CONVERSION TO YAML
250namespace YAML
251{
254template <typename T>
255struct convert<wolf::Composite<T>>
256{
259 static Node encode(const wolf::Composite<T>& composite)
260 {
261 Node n;
262 for (auto&& pair : composite)
263 {
264 n[pair.first] = pair.second;
265 }
266
267 return n;
268 }
269
272 static bool decode(const Node& node, wolf::Composite<T>& composite)
273 {
274 composite = wolf::Composite<T>(node);
275
276 return true;
277 }
278};
279} // namespace YAML
Map of state keys (char) to objects of type T (vectors, matrices, types, priors, ....
Definition: composite.h:49
Composite(const YAML::Node &_n, const std::string &_field, const bool _required, const std::string &_keys=ALL_KEYS)
Constructor from a YAML map of keys.
Definition: composite.h:105
StateKeys getKeys() const
Returns all the keys.
Definition: composite.h:216
bool has(const StateKeys &_structure) const
Returns whether the composite has all the keys.
Definition: composite.h:203
T CompositeType
Type of the stored values.
Definition: composite.h:51
virtual ~Composite()=default
Destructor.
Composite operator()(const std::string &_keys) const
Returns the composite of the given keys. Throws if any is missing.
Definition: composite.h:225
friend std::ostream & operator<<(std::ostream &_os, const Composite< T > &_x)
Prints the composite to a stream.
Definition: composite.h:84
YAML::Node toYaml() const
Returns a YAML map of the keys.
Definition: composite.h:242
bool has(char _key) const
Returns whether the composite has a key.
Definition: composite.h:197
#define WOLF_WARN(...)
Definition: logging.h:511
Definition: composite.h:251
Definition: capture_base.h:23
std::string StateKeys
Definition: composite.h:31
static std::string ALL_KEYS
Definition: composite.h:33
Composite< bool > BoolComposite
Definition: composite.h:102
StateKeys keysIntersection(const StateKeys &_keys1, const StateKeys &_keys2)
Intersection of two sets of keys: the keys of _keys1 that are also in _keys2, in the order of _keys1 ...
Definition: composite.cpp:31
StateKeys keysUnion(const StateKeys &_keys1, const StateKeys &_keys2)
Union of two sets of keys: the keys of _keys1 followed by the keys of _keys2 not in _keys1 (no duplic...
Definition: composite.cpp:23
static Node encode(const wolf::Composite< T > &composite)
Encodes a composite into a YAML node.
Definition: composite.h:259
static bool decode(const Node &node, wolf::Composite< T > &composite)
Decodes a composite from a YAML node.
Definition: composite.h:272